package com.klhappy.houyuan.activity;


import java.util.List;


import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.FamilyBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.adapter.FamilyMemberAdapter;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenu;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuCreator;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuItem;
import com.klhappy.houyuan.view.swipemenulistview.SwipeMenuListView;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.ListView;
import android.widget.TextView;

public class Family_MemberActivity extends AbsActivity implements CallBackInterModle {


    @InjectView(R.id.lv_family_member)
    SwipeMenuListView lv_family_member;
    @InjectView(R.id.title)
    TextView title;

    @OnClick(R.id.click_ra)
    public void click_ra() {
        finish();
    }

    @OnClick(R.id.add_linear)
    public void add_linear() {
        startActivity(new Intent(Family_MemberActivity.this, AddFamilyActivity.class));
    }

    private JiaYuanModle modle;
    private FamilyBean familyBean;
    private List<FamilyBean.Records> records;
    private FamilyMemberAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.family_member);
        ButterKnife.inject(this);
        title.setText(R.string.family_member);
        modle = new JiaYuanModle(this);
        modle.setCallBackInterModle(this);
        showDialog("请稍等...");

        setSwipeMenu();
    }

    @Override
    protected void onResume() {
        super.onResume();
        modle.getFamily();
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.MEMBER_URL)) {
            closeDialog();
            if (o.getCode() == 200) {
                familyBean = JsonCode.getClass(o.getEntity(), FamilyBean.class);
                records = JsonCode.getClassList(familyBean.getRecords(), FamilyBean.Records.class);

                adapter = new FamilyMemberAdapter(this, records);
                lv_family_member.setAdapter(adapter);
            }
        }else if (url.equals(UrlConstant.REFAMILY_URL)){
            if (o.getCode() == 200){
                showToast(o.getMsg());
            }
            closeDialog();
        }
    }

    private void setSwipeMenu() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(Family_MemberActivity.this);
                openItem.setBackground(new ColorDrawable(Color.rgb(0x86, 0x9e, 0x34)));
                openItem.setWidth(dp2px(68));
                openItem.setTitle("删除");
                openItem.setTitleSize(14);
                openItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(openItem);
            }
        };
        lv_family_member.setMenuCreator(creator);

        lv_family_member.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        modle.RemoveFamily(records.get(position).getOwn_id() + "");
                        records.remove(position);
                        adapter.notifyDataSetChanged();
                        showDialog("请稍等...");
                        break;
                    default:
                        break;
                }
            }
        });

        lv_family_member.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}
